package com.shijie.controller.erp;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.annotation.Log;
import com.shijie.core.annotation.RequiresPermissions;
import com.shijie.core.constants.PnConstant.BusinessType;
import com.shijie.domain.entity.Recommends;
import com.shijie.service.RecommendsService;

/*
 *  @author: smart boy
 *  @date: 2018-08-23
 */
@RestController
@RequestMapping("/erp/recommends")
public class RecommendsController {

	@Resource
	private RecommendsService recommendsService;

	@RequiresPermissions(value = { "product:rcmd" })
	@GetMapping
	public Result list(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer size,
			@RequestParam(required = false) String q,@RequestParam(required = false) Integer entityType) {
		PageHelper.startPage(page, size);
		Condition condition = new Condition(Recommends.class);
		Example.Criteria criteria = condition.createCriteria();
		if (StringUtils.isNotBlank(q)) {
			criteria.andLike("title", "%" + q + "%").orLike("subTitle",
					"%" + q + "%");
		}
		if(entityType != null){
			criteria.andEqualTo("entityType", entityType);
		}
		List<Recommends> list = recommendsService.selectByCondition(condition);
		PageInfo<Recommends> pageInfo = new PageInfo<>(list);
		return ResultGenerator.success(pageInfo);
	}

	@GetMapping("search")
	public Result search(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer size,
			@RequestParam(required = false) String q,
			@RequestParam(required = false) Integer type) {
		PageHelper.startPage(page, size);
		List list = recommendsService.search(q, type);
		PageInfo<Recommends> pageInfo = new PageInfo<>(list);
		return ResultGenerator.success(pageInfo);
	}

	@RequiresPermissions(value = { "product:rcmd:edit","product:rcmd:create" })
	@Log(title = "推荐管理", action = BusinessType.INSERT)
	@PostMapping
	public Result add(@RequestBody Recommends recommends) {
		recommendsService.save(recommends);
		return ResultGenerator.success();
	}

	@RequiresPermissions(value = { "users:comments:delete" })
	@Log(title = "推荐管理", action = BusinessType.DELETE)
	@DeleteMapping("/{id}")
	public Result delete(@PathVariable Integer id) {
		recommendsService.deleteById(id);
		return ResultGenerator.success();
	}
}
